package simple;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

@Slf4j
public class Producer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("abc");
        producer.setNamesrvAddr("localhost:9876");
        producer.setSendMsgTimeout(100* 1000);
        producer.start();
        producer.setMaxMessageSize(2);

        List<MessageQueue> queueList = producer.fetchPublishMessageQueues("TopicTest");
        log.info("queueList:{}",queueList);

        MessageQueueSelector selector = new MessageQueueSelector() {
            @Override
            public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                DefaultMQProducer producer1 = (DefaultMQProducer) arg;
                int i = producer1.getMaxMessageSize();
                return mqs.get(i);
//                return mqs.get((Integer) arg);
            }
        };
        MessageQueue queue = selector.select(queueList, new Message(), producer);
        System.out.println(queue);
    }
}
